package com.yq.cp.service.basics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.model.SiteHeaderToUser;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.mapper.basics.OrganizationalStructureMapper;
import com.yq.cp.service.basics.OrganizationalStructureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 组织架构 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class OrganizationalStructureServiceImpl extends ServiceImpl<OrganizationalStructureMapper, OrganizationalStructure> implements OrganizationalStructureService {
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;

    @Override
    public Page<OrganizationalStructure> switchSite(PageVo vo) {
        Page<OrganizationalStructure> page = new Page<>(vo.getPage(), vo.getSize());
        page = organizationalStructureMapper.switchSite(page,vo);
        return page;
    }

    @Override
    public IPage<SiteHeaderToUser> siteHeaderExamineList(PageVo vo) {
        IPage<SiteHeaderToUser> page = new Page<>(vo.getPage(),vo.getSize());
        page =organizationalStructureMapper.siteHeaderExamineList(page,vo);
        if (!page.getRecords().isEmpty()){
            for (SiteHeaderToUser record : page.getRecords()) {
                OrganizationalStructure structure = organizationalStructureMapper.selectById(record.getStructureId());
                record.setSiteName(structure.getName());
                record.setSiteId(record.getStructureId());
            }
        }
        return page;
    }


    @Override
    public BigDecimal mfxss(Long id) {

        QueryWrapper<OrganizationalStructure> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("examine",1);
        queryWrapper.eq("type",6);
        if (id!=null){
            queryWrapper.eq("id",id);
        }
        List<OrganizationalStructure> energyStations = organizationalStructureMapper.selectList(queryWrapper);
        BigDecimal installedCapacity=new BigDecimal(0);
        for (int i = 0; i < energyStations.size(); i++) {
            OrganizationalStructure organizationalStructure = energyStations.get(i);
            BigDecimal installedCapacity1 = organizationalStructure.getInstalledCapacity();
            installedCapacity1=installedCapacity1==null?new BigDecimal(0):installedCapacity1;
            installedCapacity=installedCapacity.add(installedCapacity1);

        }
        return installedCapacity;
    }

}
